package com.woniuxy.joylife.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.joylife.manager.cascaders.BlockCascader;
import com.woniuxy.joylife.manager.cascaders.RoomCascader;
import com.woniuxy.joylife.manager.cascaders.UnitCascader;
import com.woniuxy.joylife.manager.dao.BlockDao;
import com.woniuxy.joylife.manager.dao.RoomDao;
import com.woniuxy.joylife.manager.dao.UnitDao;
import com.woniuxy.joylife.manager.entity.BlockEntity;
import com.woniuxy.joylife.manager.entity.RoomEntity;
import com.woniuxy.joylife.manager.entity.UnitEntity;
import com.woniuxy.joylife.manager.service.BlockService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("blockService")
public class BlockServiceImpl extends ServiceImpl<BlockDao, BlockEntity> implements BlockService {

    @Autowired
    private UnitDao unitDao;

    @Autowired
    private RoomDao roomDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BlockEntity> page = this.page(
                new Query<BlockEntity>().getPage(params),
                new QueryWrapper<BlockEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean saveBlockEntity(BlockEntity block) {

        QueryWrapper<BlockEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",block.getName());
        BlockEntity blockEntity = baseMapper.selectOne(queryWrapper);
        if(blockEntity==null){
            baseMapper.insert(block);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<BlockCascader> getCascader() {

        ArrayList<BlockCascader> blockCascaders = new ArrayList<>();

        ArrayList<UnitCascader> unitCascaders = new ArrayList<>();

        ArrayList<RoomCascader> roomCascaders = new ArrayList<>();

        List<BlockEntity> blockEntities = baseMapper.selectList(null).stream().sorted((entity1, entity2) -> {
            return entity1.getSort() - entity2.getSort();
        }).collect(Collectors.toList());

        List<UnitEntity> unitEntities = unitDao.selectList(null).stream().sorted((entity1, entity2) -> {
            return entity1.getSort() - entity2.getSort();
        }).collect(Collectors.toList());

        List<RoomEntity> roomEntities = roomDao.selectList(null).stream().sorted((entity1, entity2) -> {
            return entity1.getSort() - entity2.getSort();
        }).collect(Collectors.toList());


        for (RoomEntity r :
                roomEntities) {
            RoomCascader roomCascader = new RoomCascader();
            roomCascader.setValue(r.getRoomId());
            roomCascader.setLabel(r.getName());
            roomCascaders.add(roomCascader);
        }

        for (int j = 0; j < unitEntities.size(); j++) {
            UnitEntity unitEntity = unitEntities.get(j);
            UnitCascader unitCascader = new UnitCascader();
            unitCascader.setValue(unitEntity.getUniId());
            unitCascader.setLabel(unitEntity.getName());
            unitCascader.setChildren(roomCascaders);
            unitCascaders.add(unitCascader);
        }

        for (int i = 0; i < blockEntities.size(); i++) {
            // 设置第一层级
            BlockEntity blockEntity = blockEntities.get(i);
            BlockCascader blockCascader = new BlockCascader();
            blockCascader.setValue(blockEntity.getBlocId());
            blockCascader.setLabel(blockEntity.getName());
            // 设置第二层级
            blockCascader.setChildren(unitCascaders);
            blockCascaders.add(blockCascader);
        }

        return blockCascaders;
    }

}